


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SubSonic.DataProviders;
using SubSonic.Extensions;
using System.Linq.Expressions;
using SubSonic.Schema;
using System.Collections;
using SubSonic;
using SubSonic.Repository;
using System.ComponentModel;
using System.Data.Common;

namespace SurveyTracker.Data
{
    
    
    /// <summary>
    /// A class which represents the Mailing table in the surveyTracker Database.
    /// </summary>
    public partial class Mailing: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<Mailing> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<Mailing>(new SurveyTracker.Data.surveyTrackerDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<Mailing> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(Mailing item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                Mailing item=new Mailing();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<Mailing> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        SurveyTracker.Data.surveyTrackerDB _db;
        public Mailing(string connectionString, string providerName) {

            _db=new SurveyTracker.Data.surveyTrackerDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                Mailing.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Mailing>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public Mailing(){
             _db=new SurveyTracker.Data.surveyTrackerDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public Mailing(Expression<Func<Mailing, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<Mailing> GetRepo(string connectionString, string providerName){
            SurveyTracker.Data.surveyTrackerDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new SurveyTracker.Data.surveyTrackerDB();
            }else{
                db=new SurveyTracker.Data.surveyTrackerDB(connectionString, providerName);
            }
            IRepository<Mailing> _repo;
            
            if(db.TestMode){
                Mailing.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Mailing>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<Mailing> GetRepo(){
            return GetRepo("","");
        }
        
        public static Mailing SingleOrDefault(Expression<Func<Mailing, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            Mailing single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static Mailing SingleOrDefault(Expression<Func<Mailing, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            Mailing single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<Mailing, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<Mailing, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<Mailing> Find(Expression<Func<Mailing, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<Mailing> Find(Expression<Func<Mailing, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<Mailing> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<Mailing> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<Mailing> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<Mailing> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<Mailing> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<Mailing> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "MailingID";
        }

        public object KeyValue()
        {
            return this.MailingID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.MailingName.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(Mailing)){
                Mailing compare=(Mailing)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.MailingID;
        }
        
        public string DescriptorValue()
        {
                            return this.MailingName.ToString();
                    }

        public string DescriptorColumn() {
            return "MailingName";
        }
        public static string GetKeyColumn()
        {
            return "MailingID";
        }        
        public static string GetDescriptorColumn()
        {
            return "MailingName";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _MailingID;
        public int MailingID
        {
            get { return _MailingID; }
            set
            {
                if(_MailingID!=value){
                    _MailingID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MailingID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _MailingName;
        public string MailingName
        {
            get { return _MailingName; }
            set
            {
                if(_MailingName!=value){
                    _MailingName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MailingName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _MailingDate;
        public DateTime? MailingDate
        {
            get { return _MailingDate; }
            set
            {
                if(_MailingDate!=value){
                    _MailingDate=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MailingDate");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _MailingSubject;
        public string MailingSubject
        {
            get { return _MailingSubject; }
            set
            {
                if(_MailingSubject!=value){
                    _MailingSubject=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MailingSubject");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _MailingAuthor;
        public string MailingAuthor
        {
            get { return _MailingAuthor; }
            set
            {
                if(_MailingAuthor!=value){
                    _MailingAuthor=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MailingAuthor");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _MailingFromEmail;
        public string MailingFromEmail
        {
            get { return _MailingFromEmail; }
            set
            {
                if(_MailingFromEmail!=value){
                    _MailingFromEmail=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MailingFromEmail");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _MailingReplyToEmail;
        public string MailingReplyToEmail
        {
            get { return _MailingReplyToEmail; }
            set
            {
                if(_MailingReplyToEmail!=value){
                    _MailingReplyToEmail=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MailingReplyToEmail");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _MailingText;
        public string MailingText
        {
            get { return _MailingText; }
            set
            {
                if(_MailingText!=value){
                    _MailingText=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MailingText");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _MailingSurveyLink;
        public string MailingSurveyLink
        {
            get { return _MailingSurveyLink; }
            set
            {
                if(_MailingSurveyLink!=value){
                    _MailingSurveyLink=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MailingSurveyLink");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<Mailing, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the MailingHistory table in the surveyTracker Database.
    /// </summary>
    public partial class MailingHistory: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<MailingHistory> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<MailingHistory>(new SurveyTracker.Data.surveyTrackerDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<MailingHistory> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(MailingHistory item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                MailingHistory item=new MailingHistory();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<MailingHistory> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        SurveyTracker.Data.surveyTrackerDB _db;
        public MailingHistory(string connectionString, string providerName) {

            _db=new SurveyTracker.Data.surveyTrackerDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                MailingHistory.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<MailingHistory>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public MailingHistory(){
             _db=new SurveyTracker.Data.surveyTrackerDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public MailingHistory(Expression<Func<MailingHistory, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<MailingHistory> GetRepo(string connectionString, string providerName){
            SurveyTracker.Data.surveyTrackerDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new SurveyTracker.Data.surveyTrackerDB();
            }else{
                db=new SurveyTracker.Data.surveyTrackerDB(connectionString, providerName);
            }
            IRepository<MailingHistory> _repo;
            
            if(db.TestMode){
                MailingHistory.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<MailingHistory>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<MailingHistory> GetRepo(){
            return GetRepo("","");
        }
        
        public static MailingHistory SingleOrDefault(Expression<Func<MailingHistory, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            MailingHistory single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static MailingHistory SingleOrDefault(Expression<Func<MailingHistory, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            MailingHistory single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<MailingHistory, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<MailingHistory, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<MailingHistory> Find(Expression<Func<MailingHistory, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<MailingHistory> Find(Expression<Func<MailingHistory, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<MailingHistory> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<MailingHistory> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<MailingHistory> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<MailingHistory> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<MailingHistory> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<MailingHistory> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "MailingListId";
        }

        public object KeyValue()
        {
            return this.MailingListId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.Status.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(MailingHistory)){
                MailingHistory compare=(MailingHistory)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.MailingListId;
        }
        
        public string DescriptorValue()
        {
                            return this.Status.ToString();
                    }

        public string DescriptorColumn() {
            return "Status";
        }
        public static string GetKeyColumn()
        {
            return "MailingListId";
        }        
        public static string GetDescriptorColumn()
        {
            return "Status";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _MailingListId;
        public int MailingListId
        {
            get { return _MailingListId; }
            set
            {
                if(_MailingListId!=value){
                    _MailingListId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MailingListId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _MailingId;
        public int? MailingId
        {
            get { return _MailingId; }
            set
            {
                if(_MailingId!=value){
                    _MailingId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MailingId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ListId;
        public int? ListId
        {
            get { return _ListId; }
            set
            {
                if(_ListId!=value){
                    _ListId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ListId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        DateTime? _MailingDate;
        public DateTime? MailingDate
        {
            get { return _MailingDate; }
            set
            {
                if(_MailingDate!=value){
                    _MailingDate=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MailingDate");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _emailId;
        public int? emailId
        {
            get { return _emailId; }
            set
            {
                if(_emailId!=value){
                    _emailId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="emailId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _Status;
        public string Status
        {
            get { return _Status; }
            set
            {
                if(_Status!=value){
                    _Status=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="Status");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _emailHashKey;
        public string emailHashKey
        {
            get { return _emailHashKey; }
            set
            {
                if(_emailHashKey!=value){
                    _emailHashKey=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="emailHashKey");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<MailingHistory, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the EmailList table in the surveyTracker Database.
    /// </summary>
    public partial class EmailList: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<EmailList> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<EmailList>(new SurveyTracker.Data.surveyTrackerDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<EmailList> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(EmailList item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                EmailList item=new EmailList();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<EmailList> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        SurveyTracker.Data.surveyTrackerDB _db;
        public EmailList(string connectionString, string providerName) {

            _db=new SurveyTracker.Data.surveyTrackerDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                EmailList.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<EmailList>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public EmailList(){
             _db=new SurveyTracker.Data.surveyTrackerDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public EmailList(Expression<Func<EmailList, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<EmailList> GetRepo(string connectionString, string providerName){
            SurveyTracker.Data.surveyTrackerDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new SurveyTracker.Data.surveyTrackerDB();
            }else{
                db=new SurveyTracker.Data.surveyTrackerDB(connectionString, providerName);
            }
            IRepository<EmailList> _repo;
            
            if(db.TestMode){
                EmailList.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<EmailList>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<EmailList> GetRepo(){
            return GetRepo("","");
        }
        
        public static EmailList SingleOrDefault(Expression<Func<EmailList, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            EmailList single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static EmailList SingleOrDefault(Expression<Func<EmailList, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            EmailList single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<EmailList, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<EmailList, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<EmailList> Find(Expression<Func<EmailList, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<EmailList> Find(Expression<Func<EmailList, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<EmailList> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<EmailList> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<EmailList> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<EmailList> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<EmailList> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<EmailList> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "EmailListId";
        }

        public object KeyValue()
        {
            return this.EmailListId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.EmailId.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(EmailList)){
                EmailList compare=(EmailList)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.EmailListId;
        }
        
        public string DescriptorValue()
        {
                            return this.EmailId.ToString();
                    }

        public string DescriptorColumn() {
            return "EmailId";
        }
        public static string GetKeyColumn()
        {
            return "EmailListId";
        }        
        public static string GetDescriptorColumn()
        {
            return "EmailId";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _EmailListId;
        public int EmailListId
        {
            get { return _EmailListId; }
            set
            {
                if(_EmailListId!=value){
                    _EmailListId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="EmailListId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _EmailId;
        public int? EmailId
        {
            get { return _EmailId; }
            set
            {
                if(_EmailId!=value){
                    _EmailId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="EmailId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _ListId;
        public int? ListId
        {
            get { return _ListId; }
            set
            {
                if(_ListId!=value){
                    _ListId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ListId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        int? _status;
        public int? status
        {
            get { return _status; }
            set
            {
                if(_status!=value){
                    _status=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="status");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<EmailList, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Emails table in the surveyTracker Database.
    /// </summary>
    public partial class Email: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<Email> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<Email>(new SurveyTracker.Data.surveyTrackerDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<Email> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(Email item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                Email item=new Email();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<Email> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        SurveyTracker.Data.surveyTrackerDB _db;
        public Email(string connectionString, string providerName) {

            _db=new SurveyTracker.Data.surveyTrackerDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                Email.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Email>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public Email(){
             _db=new SurveyTracker.Data.surveyTrackerDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public Email(Expression<Func<Email, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<Email> GetRepo(string connectionString, string providerName){
            SurveyTracker.Data.surveyTrackerDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new SurveyTracker.Data.surveyTrackerDB();
            }else{
                db=new SurveyTracker.Data.surveyTrackerDB(connectionString, providerName);
            }
            IRepository<Email> _repo;
            
            if(db.TestMode){
                Email.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<Email>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<Email> GetRepo(){
            return GetRepo("","");
        }
        
        public static Email SingleOrDefault(Expression<Func<Email, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            Email single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static Email SingleOrDefault(Expression<Func<Email, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            Email single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<Email, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<Email, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<Email> Find(Expression<Func<Email, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<Email> Find(Expression<Func<Email, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<Email> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<Email> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<Email> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<Email> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<Email> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<Email> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "emailId";
        }

        public object KeyValue()
        {
            return this.emailId;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.emailAddress.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(Email)){
                Email compare=(Email)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.emailId;
        }
        
        public string DescriptorValue()
        {
                            return this.emailAddress.ToString();
                    }

        public string DescriptorColumn() {
            return "emailAddress";
        }
        public static string GetKeyColumn()
        {
            return "emailId";
        }        
        public static string GetDescriptorColumn()
        {
            return "emailAddress";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _emailId;
        public int emailId
        {
            get { return _emailId; }
            set
            {
                if(_emailId!=value){
                    _emailId=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="emailId");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _emailAddress;
        public string emailAddress
        {
            get { return _emailAddress; }
            set
            {
                if(_emailAddress!=value){
                    _emailAddress=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="emailAddress");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _emailStatus;
        public string emailStatus
        {
            get { return _emailStatus; }
            set
            {
                if(_emailStatus!=value){
                    _emailStatus=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="emailStatus");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<Email, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
    
    
    /// <summary>
    /// A class which represents the Lists table in the surveyTracker Database.
    /// </summary>
    public partial class List: IActiveRecord
    {
    
        #region Built-in testing
        static TestRepository<List> _testRepo;
        

        
        static void SetTestRepo(){
            _testRepo = _testRepo ?? new TestRepository<List>(new SurveyTracker.Data.surveyTrackerDB());
        }
        public static void ResetTestRepo(){
            _testRepo = null;
            SetTestRepo();
        }
        public static void Setup(List<List> testlist){
            SetTestRepo();
            foreach (var item in testlist)
            {
                _testRepo._items.Add(item);
            }
        }
        public static void Setup(List item) {
            SetTestRepo();
            _testRepo._items.Add(item);
        }
        public static void Setup(int testItems) {
            SetTestRepo();
            for(int i=0;i<testItems;i++){
                List item=new List();
                _testRepo._items.Add(item);
            }
        }
        
        public bool TestMode = false;


        #endregion

        IRepository<List> _repo;
        ITable tbl;
        bool _isNew;
        public bool IsNew(){
            return _isNew;
        }
        
        public void SetIsLoaded(bool isLoaded){
            _isLoaded=isLoaded;
            if(isLoaded)
                OnLoaded();
        }
        
        public void SetIsNew(bool isNew){
            _isNew=isNew;
        }
        bool _isLoaded;
        public bool IsLoaded(){
            return _isLoaded;
        }
                
        List<IColumn> _dirtyColumns;
        public bool IsDirty(){
            return _dirtyColumns.Count>0;
        }
        
        public List<IColumn> GetDirtyColumns (){
            return _dirtyColumns;
        }

        SurveyTracker.Data.surveyTrackerDB _db;
        public List(string connectionString, string providerName) {

            _db=new SurveyTracker.Data.surveyTrackerDB(connectionString, providerName);
            Init();            
         }
        void Init(){
            TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
            _dirtyColumns=new List<IColumn>();
            if(TestMode){
                List.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<List>(_db);
            }
            tbl=_repo.GetTable();
            SetIsNew(true);
            OnCreated();       

        }
        
        public List(){
             _db=new SurveyTracker.Data.surveyTrackerDB();
            Init();            
        }
        
       
        partial void OnCreated();
            
        partial void OnLoaded();
        
        partial void OnSaved();
        
        partial void OnChanged();
        
        public IList<IColumn> Columns{
            get{
                return tbl.Columns;
            }
        }

        public List(Expression<Func<List, bool>> expression):this() {

            SetIsLoaded(_repo.Load(this,expression));
        }
        
       
        
        internal static IRepository<List> GetRepo(string connectionString, string providerName){
            SurveyTracker.Data.surveyTrackerDB db;
            if(String.IsNullOrEmpty(connectionString)){
                db=new SurveyTracker.Data.surveyTrackerDB();
            }else{
                db=new SurveyTracker.Data.surveyTrackerDB(connectionString, providerName);
            }
            IRepository<List> _repo;
            
            if(db.TestMode){
                List.SetTestRepo();
                _repo=_testRepo;
            }else{
                _repo = new SubSonicRepository<List>(db);
            }
            return _repo;        
        }       
        
        internal static IRepository<List> GetRepo(){
            return GetRepo("","");
        }
        
        public static List SingleOrDefault(Expression<Func<List, bool>> expression) {

            var repo = GetRepo();
            var results=repo.Find(expression);
            List single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
                single.OnLoaded();
                single.SetIsLoaded(true);
                single.SetIsNew(false);
            }

            return single;
        }      
        
        public static List SingleOrDefault(Expression<Func<List, bool>> expression,string connectionString, string providerName) {
            var repo = GetRepo(connectionString,providerName);
            var results=repo.Find(expression);
            List single=null;
            if(results.Count() > 0){
                single=results.ToList()[0];
            }

            return single;


        }
        
        
        public static bool Exists(Expression<Func<List, bool>> expression,string connectionString, string providerName) {
           
            return All(connectionString,providerName).Any(expression);
        }        
        public static bool Exists(Expression<Func<List, bool>> expression) {
           
            return All().Any(expression);
        }        

        public static IList<List> Find(Expression<Func<List, bool>> expression) {
            
            var repo = GetRepo();
            return repo.Find(expression).ToList();
        }
        
        public static IList<List> Find(Expression<Func<List, bool>> expression,string connectionString, string providerName) {

            var repo = GetRepo(connectionString,providerName);
            return repo.Find(expression).ToList();

        }
        public static IQueryable<List> All(string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetAll();
        }
        public static IQueryable<List> All() {
            return GetRepo().GetAll();
        }
        
        public static PagedList<List> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
        }
      
        public static PagedList<List> GetPaged(string sortBy, int pageIndex, int pageSize) {
            return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
        }

        public static PagedList<List> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
            return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);
            
        }


        public static PagedList<List> GetPaged(int pageIndex, int pageSize) {
            return GetRepo().GetPaged(pageIndex, pageSize);
            
        }

        public string KeyName()
        {
            return "ListID";
        }

        public object KeyValue()
        {
            return this.ListID;
        }
        
        public void SetKeyValue(object value) {
            if (value != null && value!=DBNull.Value) {
                var settable = value.ChangeTypeTo<int>();
                this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
            }
        }
        
        public override string ToString(){
                            return this.listName.ToString();
                    }

        public override bool Equals(object obj){
            if(obj.GetType()==typeof(List)){
                List compare=(List)obj;
                return compare.KeyValue()==this.KeyValue();
            }else{
                return base.Equals(obj);
            }
        }

        
        public override int GetHashCode() {
            return this.ListID;
        }
        
        public string DescriptorValue()
        {
                            return this.listName.ToString();
                    }

        public string DescriptorColumn() {
            return "listName";
        }
        public static string GetKeyColumn()
        {
            return "ListID";
        }        
        public static string GetDescriptorColumn()
        {
            return "listName";
        }
        
        #region ' Foreign Keys '
        #endregion
        

        int _ListID;
        public int ListID
        {
            get { return _ListID; }
            set
            {
                if(_ListID!=value){
                    _ListID=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ListID");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }

        string _listName;
        public string listName
        {
            get { return _listName; }
            set
            {
                if(_listName!=value){
                    _listName=value;
                    var col=tbl.Columns.SingleOrDefault(x=>x.Name=="listName");
                    if(col!=null){
                        if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                            _dirtyColumns.Add(col);
                        }
                    }
                    OnChanged();
                }
            }
        }



        public DbCommand GetUpdateCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();
            
        }
        public DbCommand GetInsertCommand() {
 
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
        }
        
        public DbCommand GetDeleteCommand() {
            if(TestMode)
                return _db.DataProvider.CreateCommand();
            else
                return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
        }
       
        
        public void Update(){
            Update(_db.DataProvider);
        }
        
        public void Update(IDataProvider provider){
        
            
            if(this._dirtyColumns.Count>0){
                _repo.Update(this,provider);
                _dirtyColumns.Clear();    
            }
            OnSaved();
       }
 
        public void Add(){
            Add(_db.DataProvider);
        }
        
        
       
        public void Add(IDataProvider provider){

            
            var key=KeyValue();
            if(key==null){
                var newKey=_repo.Add(this,provider);
                this.SetKeyValue(newKey);
            }else{
                _repo.Add(this,provider);
            }
            SetIsNew(false);
            OnSaved();
        }
        
                
        
        public void Save() {
            Save(_db.DataProvider);
        }      
        public void Save(IDataProvider provider) {
            
           
            if (_isNew) {
                Add(provider);
                
            } else {
                Update(provider);
            }
            
        }

        

        public void Delete(IDataProvider provider) {
                   
                 
            _repo.Delete(KeyValue());
            
                    }


        public void Delete() {
            Delete(_db.DataProvider);
        }


        public static void Delete(Expression<Func<List, bool>> expression) {
            var repo = GetRepo();
            
       
            
            repo.DeleteMany(expression);
            
        }

        

        public void Load(IDataReader rdr) {
            Load(rdr, true);
        }
        public void Load(IDataReader rdr, bool closeReader) {
            if (rdr.Read()) {

                try {
                    rdr.Load(this);
                    SetIsNew(false);
                    SetIsLoaded(true);
                } catch {
                    SetIsLoaded(false);
                    throw;
                }
            }else{
                SetIsLoaded(false);
            }

            if (closeReader)
                rdr.Dispose();
        }
        

    } 
}
